package com.hmyd.hmyd_android.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.JfMallItemDto;
import com.hmyd.hmyd_android.shop.GoodsDetailsActivity;

/**
 * 积分商城收藏商品适配器
 * 
 * @author wangk
 */
public class JfMallCollectionAdapter extends BaseAdapter {

	private Context context;
	private List<JfMallItemDto> items;
	public boolean checkShow = false;
	private List<String> ids = new ArrayList<String>();
	
	Intent intent = new Intent();

	public JfMallCollectionAdapter(Context context, List<JfMallItemDto> items) {
		this.context = context;
		this.items = items;
	}

	@Override
	public int getCount() {
		return items.size();
	}
	
	public List<String> getIds() {
		return ids;
	}

	@Override
	public Object getItem(int position) {
		return items.get(position % getCount());
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void refreshData(List<JfMallItemDto> items) {
		this.items.clear();
		this.items.addAll(items);
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			viewholder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.view_jfmall_collection_item, null);
			viewholder.item_check = (CheckBox) convertView
					.findViewById(R.id.item_check);
			viewholder.itemTitle = (TextView) convertView
					.findViewById(R.id.item_title);
			viewholder.itemIcon = (ImageView) convertView
					.findViewById(R.id.image1);
			viewholder.itemPrice = (TextView) convertView
					.findViewById(R.id.item_price);
			viewholder.orgPrice=(TextView)convertView.findViewById(R.id.old_price);
			viewholder.orgPriceHua=(RelativeLayout)convertView.findViewById(R.id.orgPriceHua);
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		final JfMallItemDto dto = items.get(position);
		viewholder.itemTitle.setText(dto.title);
		viewholder.itemPrice.setText(dto.presentPrice+"");
		String iconTag = dto.icon.replace("[^\\w]", "").trim();
		viewholder.itemIcon.setTag(iconTag);
		viewholder.itemIcon.setImageResource(R.drawable.view_no_photo);
		// 防止图片错乱、重复、闪烁
		if (viewholder.itemIcon.getTag() != null
				&& viewholder.itemIcon.getTag().equals(iconTag)) {
			AppBasic.getBitmapUtils(context).display(viewholder.itemIcon, dto.icon);
		}
		if(dto.originalPrice > 0 &&
				dto.originalPrice!=dto.presentPrice) {
			viewholder.orgPriceHua.setVisibility(View.VISIBLE);
			viewholder.orgPrice.setText(String.valueOf(dto.originalPrice));
		} else {
			viewholder.orgPriceHua.setVisibility(View.GONE);
		}
		
		final CheckBox deleteCheck = viewholder.item_check;
		OnClickListener deleteListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(deleteCheck.isChecked()) {
					deleteCheck.setChecked(false);
				} else {
					deleteCheck.setChecked(true);
				}
			}
		};
		
		OnClickListener lookDetailListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context,
						GoodsDetailsActivity.class);
				intent.putExtra("id", dto.id);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		};
		viewholder.item_check.setTag(iconTag);
		if(checkShow) {
			viewholder.item_check.setVisibility(View.VISIBLE);
			if(viewholder.item_check.getTag()!=null && viewholder.item_check.getTag().equals(iconTag)) {
				if(ids.contains(dto.id)) {
					viewholder.item_check.setChecked(true);
				} else {
					viewholder.item_check.setChecked(false);
				}
			}
			convertView.setOnClickListener(deleteListener);
		} else {
			viewholder.item_check.setVisibility(View.GONE);	
			convertView.setOnClickListener(lookDetailListener);
		}
		viewholder.item_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked) {
					if(!ids.contains(dto.id)) {
						ids.add(dto.id);
					}
				} else {
					if(ids.contains(dto.id)) {
						ids.remove(dto.id);
					}
				}
			}
		});
		
		return convertView;
	}

	class ViewHolder {
		ImageView itemIcon;
		TextView itemTitle;
		TextView itemPrice;
		CheckBox item_check;
		TextView orgPrice;
		RelativeLayout orgPriceHua;
	}

}
